/**
 * 
 */
package info.niwota.profile.util;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;

/**
 * @author qiangli
 * 
 */
public class DeviceHelper {

	public static String getPhone(Context ctx) {
		try {
			TelephonyManager tm = (TelephonyManager) ctx
					.getSystemService(Context.TELEPHONY_SERVICE);
			String phone = tm.getLine1Number();
			phone = PhoneNumberUtils.formatNumber(phone);
			return phone;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getGmail(Context ctx) {
		try {
			// return the first one as default
			AccountManager ac = AccountManager.get(ctx);
			Account[] accounts = ac.getAccountsByType("com.google");
			if (accounts != null && accounts.length > 0) {
				return accounts[0].name;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getWifiMacAddress(Context ctx) {
		try {
			WifiManager wifi = (WifiManager) ctx
					.getSystemService(Context.WIFI_SERVICE);
			return wifi.getConnectionInfo().getMacAddress();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static boolean supportBluetooth() {
		return BluetoothAdapter.getDefaultAdapter() != null;
	}

	public static String getBluetoothAddress() {
		try {
			return BluetoothAdapter.getDefaultAdapter().getAddress();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
